(in-package :context-indexing)
    
(defun iterate-ngram (n function sequence &key (step 1))
  (let ((length (length sequence)))
    (loop :for start = 0 :then (+ start step) 
          :for end = (min (+ start n) length)
          :do (funcall function (subseq sequence start end))
          :while (< end length))))
        
(defmacro do-ngram ((var n sequence &optional result) &body body)
  (destructuring-bind (n &optional (step 1)) (if (listp n) n (list n))
    `(block nil
       (iterate-ngram ,n (lambda (,var) ,@body) ,sequence :step ,step)
       ,result)))
